<template>
  <div id="app">
    <LoginRegister v-if="!isLoggedIn" @login-success="handleLoginSuccess" />
    <ChatWindow v-else @logout="handleLogout" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import LoginRegister from '@/components/LoginRegister.vue'
import ChatWindow from '@/components/ChatWindow.vue'
import request from '@/utils/request'

const isLoggedIn = ref(false)

onMounted(() => {
  checkLoginStatus()
})

const checkLoginStatus = async () => {
  // 暂时跳过登录检查，直接进入聊天界面
  isLoggedIn.value = true
  
  // 如果需要恢复登录检查，请取消下面的注释

  const token = localStorage.getItem('token')
  if (!token) {
    isLoggedIn.value = false
    return
  }

  try {
    const response = await request.get('/user/checkLogin')
    if (response.data.code === 200) {
      isLoggedIn.value = true
    } else {
      // token过期，清除本地存储
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      isLoggedIn.value = false
    }
  } catch (error) {
    console.error('检查登录状态失败:', error)
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    isLoggedIn.value = false
  }

}

const handleLoginSuccess = () => {
  isLoggedIn.value = true
}

const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  isLoggedIn.value = false
}
</script>

<style>
html,
body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
}

#app {
  height: 100vh;
  width: 100vw;
}

* {
  box-sizing: border-box;
}
</style>
